package com.ybb.production.service.impl;

import com.ybb.production.domain.GarmentProductionBianfei;
import com.ybb.production.domain.GarmentProductionProcess;
import com.ybb.production.mapper.GarmentProductionBianfeiMapper;
import com.ybb.production.mapper.GarmentProductionProcessMapper;
import com.ybb.production.responce.GarmentBianfeiCompleteDetailResp;
import com.ybb.production.service.IGarmentProductionProcessService;
import com.yubb.common.utils.DateUtils;
import com.yubb.common.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 生产工序Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-08
 */
@Service
public class GarmentProductionProcessServiceImpl implements IGarmentProductionProcessService {
    @Autowired
    private GarmentProductionProcessMapper garmentProductionProcessMapper;

    @Autowired
    private GarmentProductionBianfeiMapper garmentProductionBianfeiMapper;

    /**
     * 查询生产工序
     *
     * @param id 生产工序主键
     * @return 生产工序
     */
    @Override
    public GarmentProductionProcess selectGarmentProductionProcessById(Long id) {
        return garmentProductionProcessMapper.selectGarmentProductionProcessById(id);
    }

    /**
     * 查询生产工序列表
     *
     * @param garmentProductionProcess 生产工序
     * @return 生产工序
     */
    @Override
    public List<GarmentProductionProcess> selectGarmentProductionProcessList(GarmentProductionProcess garmentProductionProcess) {
        return garmentProductionProcessMapper.selectGarmentProductionProcessList(garmentProductionProcess);
    }

    /**
     * 新增生产工序
     *
     * @param garmentProductionProcess 生产工序
     * @return 结果
     */
    @Override
    public int insertGarmentProductionProcess(GarmentProductionProcess garmentProductionProcess) {
        garmentProductionProcess.setCreateTime(DateUtils.getNowDate());
        return garmentProductionProcessMapper.insertGarmentProductionProcess(garmentProductionProcess);
    }

    /**
     * 修改生产工序
     *
     * @param garmentProductionProcess 生产工序
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateGarmentProductionProcess(List<GarmentProductionProcess> garmentProductionProcess) {
        String userId = ShiroUtils.getUserId();
        // 分三种情况，1、没有id的是新增 2、有id的是更新 3、先查询所有数据，对比传入的数据，少了就是删除的。每种情况都需要处理编菲表
        List<Long> processIds = garmentProductionProcess.stream().map(GarmentProductionProcess::getId).collect(Collectors.toList());
        GarmentProductionProcess productionProcess = new GarmentProductionProcess();
        productionProcess.setOrderId(garmentProductionProcess.get(0).getOrderId());
        List<GarmentProductionProcess> existsList = garmentProductionProcessMapper.selectGarmentProductionProcessList(productionProcess);
        // 找到删除的id
        List<Long> extraElements = existsList.stream().map(GarmentProductionProcess::getId).collect(Collectors.toList());
        extraElements.removeAll(processIds);

        List<Long> allIds = new ArrayList<>();
        allIds.addAll(processIds);
        allIds.addAll(extraElements);
        Long one = allIds.get(0);
        GarmentProductionBianfei bianfeiParam = new GarmentProductionBianfei();
        bianfeiParam.setProcessId(one);
        List<GarmentProductionBianfei> bianfeisExists = garmentProductionBianfeiMapper.selectGarmentProductionBianfeiList(bianfeiParam);
        if (!extraElements.isEmpty()) {
            garmentProductionProcessMapper.deleteGarmentProductionProcessByIds(extraElements.toArray(new Long[0]));
            // 说明有删除的
            garmentProductionBianfeiMapper.deleteBianfeiByProcessIds(extraElements);
        }

        for (GarmentProductionProcess param : garmentProductionProcess) {
            param.setUpdateBy(userId);
            param.setUpdateTime(new Date());
            if (Objects.nonNull(param.getId())) {
                // 说明是修改，不走编菲，直接更新
                garmentProductionProcessMapper.updateGarmentProductionProcess(param);
            }else{
                // 说明是新增
                param.setCreateBy(userId);
                param.setCreateTime(new Date());
                garmentProductionProcessMapper.insertGarmentProductionProcess(param);
                // 新增编菲记录
                for (GarmentProductionBianfei bianfeisExist : bianfeisExists) {
                    bianfeisExist.setId(null);
                    bianfeisExist.setProcessId(param.getId());
                    garmentProductionBianfeiMapper.insertGarmentProductionBianfei(bianfeisExist);
                }
            }
        }
        return 1;
    }

    /**
     * 批量删除生产工序
     *
     * @param ids 需要删除的生产工序主键
     * @return 结果
     */
    @Override
    public int deleteGarmentProductionProcessByIds(Long[] ids) {
        return garmentProductionProcessMapper.deleteGarmentProductionProcessByIds(ids);
    }

    /**
     * 删除生产工序信息
     *
     * @param id 生产工序主键
     * @return 结果
     */
    @Override
    public int deleteGarmentProductionProcessById(Long id) {
        return garmentProductionProcessMapper.deleteGarmentProductionProcessById(id);
    }

    public void updateProcessQuantityByOrderId(Long orderId, long total) {
        garmentProductionProcessMapper.updateProcessQuantityByOrderId(orderId, total);
    }

    public List<Map<String, Object>> selectGarmentProductionProcessLastProcess(Set<Long> orderIds) {
        return garmentProductionProcessMapper.selectGarmentProductionProcessLastProcess(orderIds);
    }

    public List<Map<String, Object>> processCnt(Set<Long> orderIds) {
        return garmentProductionProcessMapper.processCnt(orderIds);
    }

    public List<GarmentBianfeiCompleteDetailResp> selectListByBundleNo(Long cuttingBedId) {
        return garmentProductionProcessMapper.selectListByBundleNo(cuttingBedId);
    }

    public List<GarmentBianfeiCompleteDetailResp> selectListByBundleNoAndBleNo(Long cuttingBedId) {
        return garmentProductionProcessMapper.selectListByBundleNoAndBleNo(cuttingBedId);
    }
}
